using System.Collections.Generic;
using UnityEngine;

public class ManagerPVPLevels : MonoBehaviour
{
	public List<SceneInfo> listSceneName = new List<SceneInfo>();

	public static ManagerPVPLevels instance;

	public List<LevelsInfo> levels = new List<LevelsInfo>();

	public List<MapPopularitySetting> mapPopularitySettings;

	private List<RoomInfo> roomList = new List<RoomInfo>();

	private void Awake()
	{
		if (instance == null)
		{
			Object.DontDestroyOnLoad(base.gameObject);
			instance = this;
			Save.SaveInt("PVPmapsCount", levels.Count - 1);
		}
		else
		{
			Object.Destroy(base.gameObject);
		}
	}

	private void OnDestroy()
	{
	}

	public LevelsInfo GetLevelInfo(int index)
	{
		if (index >= levels.Count)
		{
			index = levels.Count - 1;
		}
		if (index < 0)
		{
			index = 0;
		}
		return levels[index];
	}

	public LevelsInfo GetLevelInfo(string nameScene)
	{
		foreach (LevelsInfo level in levels)
		{
			if (level.nameScene.Equals(nameScene))
			{
				return level;
			}
		}
		return null;
	}

	public void InitRoomList()
	{
		roomList.Clear();
		roomList.AddRange(PhotonNetwork.GetRoomList());
	}

	public MapPopularity CheckMapPopularity(LevelsInfo level)
	{
		int num = 0;
		int num2 = 0;
		RoomLevels playerBoiler = controllerConnectPhoton.thisScript.GetPlayerBoiler();
		foreach (RoomInfo room in roomList)
		{
			num += room.playerCount;
			if (room.customProperties.ContainsValue(level.nameScene) && room.customProperties["pass"].Equals(string.Empty) && room.customProperties["minLvl"].Equals(playerBoiler.minLvl.ToString()) && room.customProperties["maxLvl"].Equals(playerBoiler.maxLvl.ToString()))
			{
				num2 += room.playerCount;
			}
		}
		float num3 = ((num == 0) ? 0f : ((float)(num2 / num * 100)));
		foreach (MapPopularitySetting mapPopularitySetting in mapPopularitySettings)
		{
			if (num3 <= (float)mapPopularitySetting.maxPercent)
			{
				return mapPopularitySetting.popularity;
			}
			BCWDebug.Log("percent " + num3 + "pop.maxPercent " + mapPopularitySetting.maxPercent);
		}
		return MapPopularity.Few;
	}

	public string CheckMapPopularityMessage(MapPopularity popularyty)
	{
		foreach (MapPopularitySetting mapPopularitySetting in mapPopularitySettings)
		{
			if (mapPopularitySetting.popularity == popularyty)
			{
				return mapPopularitySetting.message;
			}
			BCWDebug.Log(string.Concat("CheckMapPopularityMessage = ", popularyty, " element.popularity ", mapPopularitySetting.popularity));
		}
		return string.Empty;
	}

	public string GetRandomLevel()
	{
		return levels[Random.Range(0, levels.Count)].nameScene;
	}

	public string GetNameLevelByNameScene(string val)
	{
		foreach (LevelsInfo level in levels)
		{
			if (level.nameScene == val)
			{
				return level.nameLevel;
			}
		}
		return string.Empty;
	}

	public string GetRandomScene(TypeGame game)
	{
		List<string> list = new List<string>();
		foreach (SceneInfo item in listSceneName)
		{
			if (item.type == game)
			{
				list = item.nameScene;
				break;
			}
		}
		int index = Random.Range(0, list.Count);
		if (list.Count != 0)
		{
			return list[index];
		}
		return "Level1";
	}
}
